#
# makedesc.pl
#
# Generate The Wais Tree
#
# Copyright Creative Dynamics, Inc. 1995
#
#$dirn = $0;
#$dirn =~ s#(.*)/.*$#$1#;
#require "$dirn/common.pl";

$CATDB_NAME = "catalyst";
$USER = "miadmin";

#
# use the libmi stuff
#

use Mi;

$lastCategory = "";
$lastSubCategory = "";

### Set up output directory

$outputDirectory = "/tmp/wais-src";
umask(022);
mkdir($outputDirectory, 0777);		# make an output directory for book	chdir "$outputDirectory" || die "Can't cd to $outputDirectory\n";
#unlink(<*>);				# delete all existing files in the t directory
system 'rm -rf *';

{
   local($query,$num_col, @record);
   local($db, $row_desc, $result, $row, $col, $row_num, $error);

### the query

$query = "
select  p.Product_id, p.SolCompatible,	
	p.ProdCatName, p.ProdSubCat, p.ProdName, v.VendorName,
	p.ProdShortDesc, p.ProdLongDesc,
	p.ProdHomePage, p.ProdImage, p.SrcAvail, p.ProdSrcLang,
	p.ProdSpecHand,
	v.VendorAddr1, v.VendorAddr2, v.VendorAddr3, v.VendorCity,
	v.VendorPostCode,
	v.VendorState, v.VendorZip, v.VendorCountry,
	v.VendorPhone1code, v.VendorPhone1,
	v.VendorPhone2code, v.VendorPhone2,
	v.VendorFaxCode, v.VendorFax,
	v.VendorEmailCode, v.VendorEmail,
	v.VendorURL
from   Vendor v using(lock=table),
       Assoc_ProdVend a using(lock=table),
       Products p using(lock=table)
where  a.ProdId = p.Product_id
and    a.VendId = v.Vendor_id
and    p.Product_id < 200
order by p.ProdCatName, p.ProdSubCat, p.ProdName
;";

# where  p.Product_id < 50 -- use to limit
# alternate order: order by p.ProdCatName, p.ProdSubCat, p.ProdName, v.VendorName

### query the database

   ($db = Mi::open("$CATDB_NAME", "$USER", "") ) || die "Can't open Illustra database\n";

   (Mi::exec($db, "$query", 0) == 0) || die "Can't exec Illustra query\n";

   print "  Query Sent\n";

   (($result = Mi::get_result($db)) == 1) || die "Got bad result from Illustra\n";

   print "  Got Results\n";

   ($row_desc = Mi::get_row_desc_without_row($db)) || die "Error on row description \n";

   $num_col = Mi::column_count($row_desc);

   print "  Results Have $num_col Columns\n";

   $row_num = 0;
   while ($row = Mi::next_row($db, \$error))
   {
	$row_num++;
	print "   Processing row $row_num...\r";
	foreach $col (0..$num_col-1)
	{
          Mi::value($row, $col, $colval, $retlen);
          $record[$col] = $colval;
	}

### Fix the data

# map double occurrences of ' to single '

	foreach $count (2..12)
	{
		$record[$count] =~ s/\'\'/\'/g;
	}

### Trim trailing spaces
	foreach $count (2..30)
	{
		$record[$count] =~ s/ *$//g;
	}

### Fix descriptions
	if ($record[7] eq "")
	{
		$record[7] = $record[6];
	}
	&makeBullets();

### Now print out the data for this product		

	$newchapter = 0;	# not new chapter default
	if ($lastCategory ne $record[2])
	{
		if ($lastCategory ne "")
		{
			chdir "../.." ||  die "Can't cd to ../..\n";
		}
		$newchapter = 1;
		$outputChapterName = $record[2];
		$outputChapterName =~ s/ /_/g;
		$outputChapterName =~ s/\//-/g;
		$outputChapterName = substr($outputChapterName, 0, 25);
		mkdir($outputChapterName, 0777);
		chdir "$outputChapterName" || die "Can't cd to $outputChapterName\n";
		unlink(<*>);
		if ($lastCategory ne "")
                {
			addsubTOCtail();
			close subTOC;
                }
		$lastCategory = $record[2];
		$subTOCname = $outputChapterName . ".html";
		open(subTOC, "> $subTOCname");
		&addsubTOCheader($record[2]);
	}
	if ($lastSubCategory ne $record[3])
	{
		if ($newchapter eq 0)
		{
			&makeTOCtail();
			close TOC;
			chdir ".." || die "Can't cd to ..\n";
		}
		$outputSubchapterName = $record[3];
		$outputSubchapterName =~ s/ /_/g;
		$outputSubchapterName =~ s/\//-/g;
		$outputSubchapterName = substr($outputSubchapterName, 0, 25);
		mkdir($outputSubchapterName, 0777);
		chdir "$outputSubchapterName" || die "Can't cd to terName\n";
		unlink(<*>);
		$lastSubCategory = $record[3];
		$TOCname = $outputSubchapterName . ".html";
		open(TOC, "> $TOCname"); 
                &makeTOCheader($subTOCname, $record[2], $record[3]);
                chdir("..");
                &addsubTOC($record[2], $record[3], $outputSubchapterName);
                chdir "$outputSubchapterName";
	}

	$filename = $record[4];
	$filename = substr($filename, 0, 25);
	$filename =~ s/ /_/g;
	$filename =~ s/\//-/g;
	$filename .= ".html";
	open(PRODUCT, "> $filename");
	&makeproductheader($record[3], $outputChapterName . ".html");
	&makeproduct();
	&makeproducttail();
	&addTOCinfo($filename, $record[4], $record[5]);
	close PRODUCT;

# End of Product

   }

   &makeTOCtail();
   close TOC;
   close subTOC;
   Mi::close($db);

   print "Processed $row_num records\n";
}

print "Complete\n";


#------------------------------------------------------------------
# SUBROUTINES
#------------------------------------------------------------------

#------------------------------------------------------------------
# makeproductheader 
#
# subroutine makeproductheader creates header for every product file

sub makeproductheader {

    local($string) = @_[0];
    local($filename) = @_[1]; 
 
    print PRODUCT <<PRODUCTHEADER;
<html>
<head>
<title>Solaris - $string</title>
</head>
<body>
<A HREF="http://pinatubo/test.html"><IMG SRC="/goto_toc.gif"> Catalyst Catalog Search Page</A> <A HREF=../$filename><IMG SRC="/goto_back.gif"> Sub-category</A>
<HR>
<H2><A NAME=record[3]>"$record[3]"</A></H2>   

<HR>
PRODUCTHEADER
}

#------------------------------------------------------------------
# makeproduct
#
# subroutine makeproduct creates info for every product file
 
sub makeproduct {

    local($source) = "No";
    local($address) = $record[13] . " " . $record[14] . " " . $record[15];
    local($address1) = $record[16] . ", " . $record[17] . $record[18] . " " . $record[19];
    local($spec) = $record[12];
    local($phone) = "";
    local($phone1) = "";
    local($fax) = "";
    local($email) = "";
    local($url) = "";
    if ($record[22] ne "")
    {
        $phone = "Phone:  " . $record[21] . $record[22];
        $phone1 = $record[23] . $record[24];
    }
    if ($record[26] ne "")
    {
        $fax = "Fax:    " . $record[25] . $record[26];
    }
    if ($record[28] ne "")
    {
        $email = "Email:  " . $record[27] . $record[28];
    }
    if ($record[29] ne "")
    {
        $url = "Url:    " . $record[29];
    }
    $address =~ s/^//g;
    $address1 =~ s/^//g;
    $spec =~ s/\[//g;
    $spec =~ s/]//g;
    $spec =~ s/^ * //g;
    $spec =~ s/^,//g;
    if ($spec eq "")
    {
        $spec = "None.";
    }

    if ($phone1 ne "")
    {
        $phone .= "\n        " . $phone1;
    }

    if ($record[10] eq "t" or $record[10] eq "T")
    {
	$source = "Yes";
    }
    print PRODUCT <<MAKEPRODUCT
<H3><A NAME=$record[4]>$record[4]</A></H3>
<H4><A NAME=$record[5]>$record[5]</A></H4>
$record[7]<p>
Language: $record[11]<BR>
Source Avail: $source<BR>
Product Special Handling: $spec<BR>
<HR>
<PRE>
<B>$record[5]</B>
$address
$address1
$record[20]
$phone
$fax
$email
$url
</PRE>
MAKEPRODUCT
}
 
#------------------------------------------------------------------ 
# makeproducttail
# 
# subroutine makeproducttail creates tail for product file
  
sub makeproducttail {
 
    print PRODUCT <<MAKEPRODUCTTAIL 
</body>
</html>
MAKEPRODUCTTAIL
} 
  

#------------------------------------------------------------------
# makeTOCheader
#
# subroutine MakeTOCheader prints header for each TOC

sub makeTOCheader {

    local($filename) = @_[0];
    local($category) = @_[1];

